from django.contrib.auth.hashers import make_password
from django.db import models


# Create your models here.
class User(models.Model):
    name = models.CharField(max_length=10,verbose_name='姓名',null=False)
    sex = models.CharField(max_length=5,verbose_name='性别')
    password = models.CharField(max_length=120,verbose_name='密码')
    phone = models.CharField(max_length=11,verbose_name='电话',unique=True)
    icon = models.TextField(verbose_name='用户图片',null=True)
    register_date = models.DateField(verbose_name='注册时间',auto_now_add=True,null=True)
    is_login = models.BooleanField(verbose_name='登录状态',default=False)
    user_type = models.IntegerField(choices=[(0,'普通用户'),(1,'VIP')],default=0)

    class Meta:
        db_table = 'tb_user'
        verbose_name_plural = verbose_name = '用户表'
        ordering = ('-register_date',)
    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        super(User, self).save()
        if len(self.password)<50:
            self.password = make_password(self.password)

    def __str__(self):
        return self.name

class UserToken(models.Model):
    user = models.OneToOneField(to='User',verbose_name='用户',on_delete=models.CASCADE)
    token = models.CharField(max_length=128,verbose_name='令牌')
    class Meta:
        db_table = 'tb_token'
        verbose_name_plural = verbose_name = '用户令牌表'

class Store(models.Model):
    category = models.ForeignKey('GoodCategory',on_delete=models.CASCADE,
                                 related_name='store')
    name = models.CharField(max_length=10,verbose_name='店铺名',null=False)
    score = models.IntegerField(verbose_name='评分')
    icon = models.ImageField(upload_to='store/imgs',verbose_name='店铺图片')
    level= models.CharField(max_length=10,verbose_name='品牌等级')# 评分高于8分为金
    sale_num = models.IntegerField(verbose_name='销量',blank=True)

    class Meta:
        db_table = 'tb_store'
        verbose_name_plural = verbose_name = '店铺表'
        ordering = ('-level',)

    def __str__(self):
        return self.name

class Good(models.Model):
    name = models.CharField(max_length=10, verbose_name='商品名', null=False)
    price = models.FloatField(verbose_name='价格')
    comment = models.CharField(max_length=50, verbose_name='商品简介')
    balance = models.IntegerField(verbose_name='库存', default=100)

    class Meta:
        db_table = 'tb_good'
        verbose_name_plural = verbose_name = '商品表'
        ordering = ('-price',)
    def __str__(self):
        return self.name

class Comment(models.Model):
    user = models.ForeignKey('User',on_delete=models.CASCADE,verbose_name='用户')
    store = models.ForeignKey('Store',on_delete=models.CASCADE,verbose_name='店铺')

    content = models.CharField(max_length=50,verbose_name='评论内容',null=True)
    score = models.IntegerField(verbose_name='评分等级',default=5,blank=True)
    time = models.DateField(verbose_name='评论时间',auto_now_add=True,null=True)

    class Meta:
        db_table = 'tb_comment'
        verbose_name_plural = verbose_name = '评论表'

# 总分类表
class GoodCategory(models.Model):

    name = models.CharField(max_length=10, verbose_name='商品分类名', null=False)
    icon = models.ImageField(upload_to='category/imgs',verbose_name='分类图片',null=True)

    class Meta:
        db_table = 'tb_category'
        verbose_name_plural = verbose_name = '商品分类表'

    def __str__(self):
        return self.name

class SubCategory(models.Model):
    name = models.CharField(max_length=10, verbose_name='子商品分类', null=False)
    icon = models.ImageField(upload_to='subcategory/imgs', verbose_name='分类图片',null=True)

    class Meta:
        db_table = 'tb_subcategory'
        verbose_name_plural = verbose_name = '子商品分类表'

class UserAddr(models.Model):
    user = models.OneToOneField('User',on_delete=models.CASCADE)
    address = models.CharField(max_length=50,verbose_name='用户地址')

    class Meta:
        db_table = 'tb_addr'
        verbose_name_plural = verbose_name = '用户地址表'
